<!-- 商品管理界面 -->
<%@ page language="java"  pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<script type="text/javascript" charset="utf-8">
var datagrid;
	$(function() {
		editRow=undefined;
		datagrid=$('#admin_spgl_datagrid').datagrid({
			url : ' ${pageContext.request.contextPath}/goodAction!datagrid.action ',
			title : '',
			iconCls : 'icon-save',
			pagination : true,
			pageSize : 5,
			pageList : [ 5, 10, 15, 20 ],
			fit : true,
			fitColumns : true,
			nowrap : false,
			border : false,
			idField : 'id',
			checkOnSelect:true,
			selectOnCheck:true,
			/* frozenColumns:[[{
                field : 'name',
                title : '登录名',
                width : 100 
                }]], */
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 100,
				checkbox:true
			}, {
				field : 'code',
				title : '商品条形码编码',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填'
						}
			           }
			}, {
				field : 'name',
				title : '商品名称',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填'
						}
			           }
			}, {
				field : 'unit',
				title : '商品单位',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填'
						}
			           }
			},{
				field : 'price',
				title : '商品单价',
				width : 100,
				sortable:true,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填'
						}
			           }
			},{
				field : 'typeid',
				title : '商品类型ID',
				width : 100,
				hidden : true
			},{
				field : 'type',
				title : '商品类型',
				width : 100,
				editor:{
					type:'combotree',
					options:{
						required:true,
						missingMessage:'该项必填'
							}
			           }
			},{
				field : 'info',
				title : '商品信息',
				width : 100,
				editor:{
					type:'validatebox'
			           }
			} ] ],
			toolbar:'#toolbar',
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
	});
		$('#admin_spgl_searchbox').searchbox({
		    searcher:function(value,name){
		    	searchFun();
		    },
		    menu:'#mm',
		    prompt:'可进行模糊查询'
		});
		editFun=function() {
			var rows = datagrid.datagrid('getSelections');
			if (rows.length == 1) {
				var p = parent.dialog({
					title : '编辑商品',
					href : '${pageContext.request.contextPath}/goodAction!goodEdit.action',
					width : 775,
					height : 245,
					buttons : [ {
						text : '编辑',
						iconCls :'icon-edit',
						handler : function() {
							var f = p.find('form');
							f.form('submit', {
								url : '${pageContext.request.contextPath}/goodAction!editor.action',
								success : function(d) {
									var json = $.parseJSON(d);
									if (json.success) {
										datagrid.datagrid('reload');
										p.dialog('close');
									}
									parent.messagerShow({
										msg : json.msg,
										title : '提示'
									});
								}
							});
						}
					} ],
					onLoad : function() {
						var f = p.find('form');
						f.form('load', {
							id : rows[0].id,
							code : rows[0].code,
							name : rows[0].name,
							unit : rows[0].unit,
							typeid : rows[0].type,
							price : rows[0].price,
							info : rows[0].info
						});
					}
				});
			} else if (rows.length > 1) {
				 $.messager.alert('提示', '同一时间只能编辑一条记录！', 'error');
			} else {
				 $.messager.alert('提示', '请选择要编辑的记录！', 'error');
			}
		};
	
		addFun=function() {
			var p = parent.dialog({
				title : '添加商品',
				href : '${pageContext.request.contextPath}/goodAction!goodAdd.action',
				width : 775,
				height : 245,
				buttons : [ {
					text : '添加',
					iconCls :'icon-add',
					handler : function() {
						var f = p.find('form');
						f.form('submit', {
							url : '${pageContext.request.contextPath}/goodAction!add.action',
							success : function(d) {
								var json = $.parseJSON(d);
								if (json.success) {
									datagrid.datagrid('reload');
									p.dialog('close');
								}
								parent.messagerShow({
									msg : json.msg,
									title : '提示'
								});
							}
						});
					}
				} ]
			    
			});
		};
		delFun=function(){
			var rows=$('#admin_spgl_datagrid').datagrid('getChecked');
			if(rows.length>0){
				$.messager.confirm("请确认","您确定要删除当前所选择的商品记录吗?",function(b){
					if(b){
						var ids=[];
						for(var i=0;i<rows.length;i++){
							ids.push(rows[i].id);
						}
						//console.info(ids.join(','));
						$.ajax({
							url:'goodAction!del.action',
							data:{
								ids:ids.join(',')
								},
							dataType:'json',
							success:function(r){
								if(r&&r.success){
									$('#admin_spgl_datagrid').datagrid('load');
									$('#admin_spgl_datagrid').datagrid('unselectAll');
								    $.messager.show({
								    	title:'提示',
								    	msg:r.msg
								    	});
								}else{
									$('#admin_spgl_datagrid').datagrid('rejectChanges');
									$('#admin_spgl_datagrid').datagrid('unselectAll');
								    $.messager.alert('错误',r.msg,'error');
								}
							}
						});
					}
				});
			}else{
				  $.messager.alert('警告','未选中任何记录,请选择要删除的记录!','warning');			
			} 
		};
		refresh=function(){
			 $('#admin_spgl_datagrid').datagrid('reload');
		};
		undoEdit=function(){
			editRow=undefined;
			$('#admin_spgl_datagrid').datagrid('rejectChanges');
			$('#admin_spgl_datagrid').datagrid('unselectAll');
		};
	function searchFun() {
		//console.info(serializeObject($('#admin_yhgl_searchForm')));
		$('#admin_spgl_datagrid').datagrid('load',serializeObject($('#admin_spgl_searchForm')));
	}
	function clearFun() {
		$('#admin_spgl_layout input[name=code]').val('');
		$('#admin_spgl_datagrid').datagrid('load', {});
	}
});
</script>
<div id="admin_spgl_layout" class="easyui-layout" data-options="border:false,fit:true">
<div region="north" data-options="border:false,title:'过滤信息'" style="height:65px;overflow:hidden;">
<form id="admin_spgl_searchForm">
<div style="float:left;margin:1px;"><input id="admin_spgl_searchbox" style="width:300px;"></input>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="clearFun();">回显所有</a></div>
</form>
<div id="mm" style="width:120px">
    <div data-options="name:'code',iconCls:'icon-ok'">条形码</div>
    <div data-options="name:'name',iconCls:'icon-ok'">名称</div>
</div>
</div>
<div region="center" data-options="border:false">
<table id="admin_spgl_datagrid"></table>
</div>
	<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
	        <shiro:hasPermission name="sys:spgl:add">
			<div onclick="addFun();" data-options="iconCls:'icon-add'">增加</div>
			</shiro:hasPermission>
			<shiro:hasPermission name="sys:spgl:edit">
			<div onclick="editFun();" data-options="iconCls:'icon-edit'">编辑</div>
			</shiro:hasPermission>
	</div>
<div id="toolbar" style="display: none;">
		<table>
			<tr>
				<shiro:hasPermission name="sys:spgl:add">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addFun();">添加</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:spgl:del">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="delFun();">删除</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:spgl:edit">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editFun();">修改</a></td>
				</shiro:hasPermission>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="undoEdit();">取消编辑</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="refresh();">刷新</a></td>
			</tr>
		</table>
	</div>
</div>


